<template>
  <!-- 这个是弹框的形式新增数据-->
  <BasicModal v-bind="$attrs" @register="registerModal" @ok="handleSubmit">
    <BasicForm @register="registerForm">
    </BasicForm>
  </BasicModal>
</template>

<script lang="ts">
import { defineComponent, } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';

import { formSchema } from './data';
import { add, update } from '/@/api/work/workrole';

export default defineComponent({
  name: 'AddModal',
  components: { BasicModal, BasicForm, },
  emits: ['success', 'register'],
  setup(_, { emit }) {

    const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
      labelWidth: 100,
      schemas: formSchema,
      showActionButtonGroup: false,
      actionColOptions: {
        span: 23,
      },
    });

    const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
      console.log('数据: ', data.record);
      resetFields();
      setModalProps({ confirmLoading: false, title: '新增离职交接' });
      setFieldsValue({
        ...data.record,
      });

    });


    async function handleSubmit() {
      try {
        setModalProps({ confirmLoading: true });
        const values = await validate();
        console.log(values);
        if (values.id) {
          update(values);
        } else {
          add(values);
        }
        closeModal();
        emit('success');
      } finally {
        setModalProps({ confirmLoading: false });
      }
    }

    return { registerModal, registerForm, handleSubmit };
  },
});
</script>
